package br.gov.cemaden.cliente.precipitacao;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author alan.oliveira
 */
public class EntityManagerWrapper {
    
    private EntityManager em;
    private final EntityManagerFactory emf;

    public EntityManagerWrapper(String persistenceUnitName) {
        this.emf = Persistence.createEntityManagerFactory(persistenceUnitName);        
    }        
    
    public synchronized EntityManager getEntityManager(){
        
        if(em != null){
            if(em.isOpen())
                return em;
        }
        
        this.em = emf.createEntityManager();
        return em;
    }
    
}
